package com.meiwenhui.sishuwujing.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.meiwenhui.sishuwujing.entities.BookExample;
import com.meiwenhui.sishuwujing.entities.Catalogue;
import com.meiwenhui.sishuwujing.entities.ChapterExample;

@Controller()
public class IndexController extends BaseController {

	@RequestMapping("/")
	public ModelAndView index() {
		ModelAndView modelAndView = new ModelAndView("/index");
		catalogues = catalogueServiceImpl.selectByExample(null);
		for (Catalogue _catalogue : catalogues) {
			BookExample bookExample = new BookExample();
			bookExample.createCriteria().andCatalogueidEqualTo(_catalogue.getId());
			bookExample.setOrderByClause(" id desc");
			_catalogue.setBooks(bookServiceImpl.selectByExample(bookExample));
		}
		modelAndView.addObject("catalogues", catalogues);
		return modelAndView;
	}
	
	@RequestMapping("/book/{name}")
	public ModelAndView chapter(@PathVariable("name") String name) {
		ModelAndView modelAndView = new ModelAndView("/chapter");
		ChapterExample chapterExample = new ChapterExample();
		chapterExample.createCriteria().andBookEqualTo(name);
		chapters = chapterServiceImpl.selectByExample(chapterExample);
		count = chapterServiceImpl.countByExample(chapterExample);
		modelAndView.addObject("chapters", chapters);
		modelAndView.addObject("count", count);
		return modelAndView;
	}
	
	@RequestMapping("/sync/{name}")
	public void sync(@PathVariable("name") String name) {
		ChapterExample chapterExample = new ChapterExample();
		chapterExample.createCriteria().andBookEqualTo(name);
		chapters = chapterServiceImpl.selectByExample(chapterExample);
	}

}
